package com.powergisol.gis.service.impl;


import com.powergisol.gis.dao.*;
import com.powergisol.gis.entity.*;
import com.powergisol.gis.service.TabDataSettingService;
import com.powergisol.gis.util.FileUtil;
import com.powergisol.gis.util.Result;
import com.powergisol.gis.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

@Service
public class TabDataSettingServiceImpl implements TabDataSettingService {
    @Autowired
    private TabDataSettingMapper settingMapper;

    @Autowired
    private TabZkMapper tabZkMapper;

    @Autowired
    private TabLayerMapper tabLayerMapper;
    @Autowired
    private TabTreeMapper tabTreeMapper;
    @Resource
    private TabSpaceMapper spaceMapper;
    @Resource
    private TabDrawStyleMapper drawStyleMapper;
    @Autowired
    private TabPileMapper tabPileMapper;
    @Autowired
    private TabRectangularPillarMapper rectangularPillarMapper;
    @Autowired
    private TabGriderMapper griderMapper;
    @Autowired
    private TabGriderHighwidthMapper griderHighwidthMapper;
    @Autowired
    private TabGriderSteadyMapper griderSteadyMapper;

    @Autowired
    private TabMarkerColorMapper tabMarkerColorMapper;
    @Autowired
    private BoardBasisMapper boardBasisMapper;

    @Autowired
    private TabWall1Mapper tabWallMapper;

    @Override
    public List<Object> listByServiceName(String serviceName, String pipeLx, Integer userId) {
       // System.out.println("图层数据加载"+serviceName);
        //查询服务类型
        TabServiceVo tabServiceVo = settingMapper.getByServiceName(serviceName);
        if (null == tabServiceVo) {
            return null;
        }
        List<Object> resultList = new ArrayList<>();
        switch (tabServiceVo.getServiceType()) {
            case 11://钻孔
                resultList = settingMapper.listByServiceName(serviceName, userId, tabServiceVo.getServiceType());
                break;
            case 10://管网
                //根据服务信息查询管网导入批次id
                String settingId = settingMapper.getSettingIdByService(serviceName, userId,
                        tabServiceVo.getServiceType());
                resultList = settingMapper.listPipe(settingId, pipeLx);

                /*for (int j = 0; j < resultList.size(); j++) {
                    for (int n = j + 1; n < resultList.size(); n++) {
                        Map<String,Object> mapj = (Map<String,Object>)resultList.get(j);
                        Map<String,Object> mapn = (Map<String,Object>)resultList.get(n);
                        if (null !=mapj.get("connectStr")&& null != mapn.get("preStr") && mapj.get("connectStr")
                        .equals(mapn.get("preStr"))) {
                            resultList.remove(n);
                        }
                    }
                }*/
//                resultList = resultList.stream().limit(100).collect(Collectors.toList());
                for (int i = 0; i < resultList.size(); i++) {
                    Map<String, Object> mapj = (Map<String, Object>) resultList.get(i);
                    if (mapj.get("pipe_size") == null || mapj.get("pipe_x") == null || mapj.get("pipe_y") == null ||
                            mapj.get("connect_x") == null || mapj.get("connect_y") == null || mapj.get("pipe_x") == mapj.get("connect_x") && mapj.get("pipe_y") == mapj.get("connect_y")) {
                        resultList.remove(i);
                        i--;
                    }
                }
                break;
            case 9://普通selectDetails
                resultList = settingMapper.listExcelBase(tabServiceVo.getUuid(),userId);
                break;
            case 12://岩层
                List<Object> map = settingMapper.listYcByServiceName(serviceName, userId,
                        tabServiceVo.getServiceType());
                for (Object o : map) {
                    Map<String, Object> m = (Map<String, Object>) o;
                    //获取岩层相关值文件路径读取返回
                    String wall = m.get("wall").toString();
                    System.out.println("wall=="+wall);
                    m.put("wall", FileUtil.getContent(wall));
                    String plane = m.get("plane").toString();
                    System.out.println("plane=="+plane);
                    m.put("plane", FileUtil.getContent(plane));
                    resultList.add(m);
                }
                break;
            case 13://点
            case 17://线
            case 18://面数据
            case 24:
                //绘制信息
                TabSceneDrawVo vo = settingMapper.getSceneDrawByServiceName(serviceName);
                if (vo.getAngle() != null && StringUtils.isNotBlank(vo.getAngle())) {
                    List<String> angle = Arrays.asList(vo.getAngle().split(","));
                    List<BigDecimal> tempList = new ArrayList<>();
                    angle.forEach(a -> {
                        tempList.add(new BigDecimal(a));
                    });
                    vo.setAngleTwo(tempList);
                }
                //经纬度
                List<LatitudeLongitude> latitudeLongitudes = spaceMapper.selectByDrawId(vo.getId());
                vo.setLatitudeLongitude(latitudeLongitudes);
                //风格样式
                vo.setDrawStyle(drawStyleMapper.selectByDrawId(vo.getId()));
                resultList.add(vo);
                break;
            case 14://桩数据
                //根据uuid查询
                resultList = tabPileMapper.listPile(tabServiceVo.getUuid());
                break;
            case 15://梁数据
                //根据uuid查询
                resultList = tabPileMapper.listBeam(tabServiceVo.getUuid());
                break;
            case 16://柱数据
                //根据uuid查询
                resultList = tabPileMapper.listPillar(tabServiceVo.getUuid());
                break;
            case 19://矩形柱数据
                resultList = rectangularPillarMapper.getListByUuid(tabServiceVo.getUuid());
                break;
            case 22://墙柱数据
                resultList = tabWallMapper.getListByUuid(tabServiceVo.getUuid());
                break;
            case 20://梁 数据
                List<TabGriderVO> resultVOList = new ArrayList<>();
                //查询梁基础信息
                List<TabGrider> griderList = griderMapper.selectByUUid(tabServiceVo.getUuid());
                //查询梁 连续梁高、宽数据
                List<TabGriderHighwidth> griderHighwidths =
                        griderHighwidthMapper.newSelectByHcdm02(tabServiceVo.getUuid());
                //查询梁 支座信息
                List<TabGriderSteady> griderSteadyList = griderSteadyMapper.newSelectByHcdm02(tabServiceVo.getUuid());
                //组装数据
                for (TabGrider e : griderList) {
                    TabGriderVO vo1 = new TabGriderVO();
                    BeanUtils.copyProperties(e, vo1);//copy

                    List<TabGriderHighwidth> list1 = new ArrayList<>();
                    List<TabGriderSteady> list2 = new ArrayList<>();
                    //相同hcdm02连续梁高宽数据
                    for (TabGriderHighwidth highwidth : griderHighwidths) {
                        if (e.getHcdm02().equals(highwidth.getHcdm02())) {
                            list1.add(highwidth);
                        }
                    }
                    //相同hcdm02支座数据
                    for (TabGriderSteady griderSteady : griderSteadyList) {
                        if (e.getHcdm02().equals(griderSteady.getHcdm02())) {
                            list2.add(griderSteady);
                        }
                    }
                    //组装
                    vo1.setGriderHighWidthList(list1);
                    vo1.setGriderSteadyList(list2);
                    resultVOList.add(vo1);
                }
                resultList.add(resultVOList);
                break;
            case 21:
                resultList = boardBasisMapper.getListByUUid(tabServiceVo.getUuid());
                break;
        }

        return resultList;
    }

    @Override
    public List<Object> listByService(SearchConditionVo vo) {
        return settingMapper.listService(vo);
    }

    @Override
    public List<TabMarkerColor> listByMarkers(String uuid) {
        return settingMapper.listByMarkers(uuid);
    }

    @Override
    public TabMarkerColor getMarkersByCondition(String uuid,String tcmc) {
        return settingMapper.getMarkersByCondition(uuid,tcmc);
    }

    @Override
    public List<TabMarkerColor> listByMarkerUndup(String uuid) {
        return settingMapper.listByMarkerUndup(uuid);
    }

    @Override
    public List<TcmcDetailVo> listTcmc(String settingId) {
        return settingMapper.listTcmc(settingId);
    }

    @Override
    public Result setMarkerColor(List<TabMarkerColor> colors) {
        Result result = new Result();
        for (TabMarkerColor color : colors) {
            if (null == color.getId()) {
                settingMapper.saveMarkerColor(color);
            } else {
                settingMapper.upMarkerColor(color);
            }
        }
        return result.success("设置成功");
    }


    @Override
    public Map<String, Object> getDetailForZk(Integer settingId, Integer dataIndex, String ZKBH,String ZKX,String ZKY) {
        Map<String, Object> map = new HashMap<>();
        List<TabZuankong> zk = tabZkMapper.getZk(settingId, ZKBH,ZKX,ZKY);
        map.put("zk", zk);
        List<TabZkTuceng> tc = tabZkMapper.getTc(settingId, ZKBH,ZKX,ZKY);
        map.put("tc", tc);
        List<TabZkShuiwei> sw = tabZkMapper.getSw(settingId, ZKBH,ZKX,ZKY);
        map.put("sw", sw);
        List<TabProject> gc = tabZkMapper.getP(settingId);
        map.put("gc", gc);
        return map;
    }

    @Override
    public Result setStaticMarkers(TabMarkerStaticColor color) {
        Result result = new Result();
        settingMapper.saveMarkerStaticColor(color);
        return result.success("设置成功");
    }

    @Override
    public List<TabMarkerStaticColor> listStaticMarker() {
        return settingMapper.listStaticMarker();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Result delZkByServiceName(String serviceName) {
        Result result = new Result();
        try {
            TabServiceVo vo = settingMapper.getByServiceName(serviceName);
            if (null == vo) {
                return result.failure(404, "数据不存在");
            }
            //删除服务、服务用户中间表
            settingMapper.delUserService(vo.getId());
            settingMapper.delTabService(vo.getId());
//            //删除钻孔设置、钻孔、土层、水位、项目
            TabDataSetting tabDataSetting = settingMapper.getByServiceId(vo.getId());
            settingMapper.delTabDataSetting(vo.getId());
            settingMapper.delTabZk(tabDataSetting.getId());
            settingMapper.delTabSw(tabDataSetting.getId());
            settingMapper.delTabTc(tabDataSetting.getId());
            settingMapper.delTabProject(tabDataSetting.getId());
            //删除钻孔设置的岩层颜色
            tabMarkerColorMapper.deleteMarker(tabDataSetting.getUuid());

            //删除layer、场景layer中间表、树中对应的layer
            List<TabLayer> tabLayers = tabLayerMapper.getByServiceId(vo.getId());
            for (TabLayer layer : tabLayers) {
                tabLayerMapper.deleteLayerById(layer.getId());
                tabLayerMapper.deleteSceneLayerById(layer.getId());
                tabTreeMapper.delTreeByLayerId(layer.getId());
            }
        } catch (Exception e) {
            result.failure(500, "删除异常");
        }
        return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Result delDataByServiceName(String serviceName) {
        Result result = new Result();
        try {
            TabServiceVo vo = settingMapper.getByServiceName(serviceName);
            if (null == vo) {
                return result.failure(404, "数据不存在");
            }
            Integer serviceType = vo.getServiceType();
            switch (serviceType){
                case 14://桩数据
                    //tabPileMapper
                    tabPileMapper.delTabPile(vo.getUuid());
                    tabPileMapper.delTabPileOther(vo.getUuid());
                    tabPileMapper.delTabPileReinforcement(vo.getUuid());
                    tabPileMapper.deldelTabPileReinforcementDetails(vo.getUuid());
                    break;
                case 15://梁数据
                    //tabPileMapper
                    tabPileMapper.delTabBeam(vo.getUuid());
                    break;
                case 16://柱数据
                    //tabPileMapper
                    tabPileMapper.delTabPillar(vo.getUuid());
                    break;
                case 19://矩形柱数据
                    //rectangularPillarMapper
                    rectangularPillarMapper.delTabRectangularPillar(vo.getUuid());
                    break;
                case 22://墙柱数据
                    //tabWallMapper
                    tabWallMapper.delTabWalld(vo.getUuid());
                    break;
                case 20://梁 数据
                    //griderMapper
                    griderMapper.delTabGrider(vo.getUuid());
                    griderMapper.delTabGriderHighwidth(vo.getUuid());
                    griderMapper.delTabGriderSteady(vo.getUuid());
                    break;
                case 21:
                    //boardBasisMapper
                    boardBasisMapper.delTabBoardBasis(vo.getUuid());
                    boardBasisMapper.deltabBoardDetailed(vo.getUuid());
                    break;
            }

            //删除服务、服务用户中间表、tabsetting表
            settingMapper.delUserService(vo.getId());
            settingMapper.delTabService(vo.getId());
            settingMapper.delTabDataSetting(vo.getId());

            //删除layer、场景layer中间表、树中对应的layer
            List<TabLayer> tabLayers = tabLayerMapper.getByServiceId(vo.getId());
            for (TabLayer layer : tabLayers) {
                tabLayerMapper.deleteLayerById(layer.getId());
                tabLayerMapper.deleteSceneLayerById(layer.getId());
                tabTreeMapper.delTreeByLayerId(layer.getId());
            }
        } catch (Exception e) {
            result.failure(500, "删除异常");
        }
        return result;
    }

    @Override
    public List<TabMarkerColor> listMarkersByUuid(String uuid) {
        List<TabMarkerColor> colors = settingMapper.listMarkers(uuid);
        return colors;
    }

    @Override
    public List<TabMarkerColor> getMarkerDetail(String uuid, String zkfc) {
        List<TabMarkerColor> colors = settingMapper.getMarkerDetail(uuid, zkfc);
        return colors;
    }

    @Override
    public List<TabDataSetting> getSetIdBySerID(List<Integer> serviceID) {

        List<TabDataSetting> dataSettings = settingMapper.getSetIdBySerID(serviceID);
        return dataSettings;
    }

    @Override
    public TabDataSetting getOne(Integer settingId) {
        return this.settingMapper.getOne(settingId);
    }

    @Override
    public Map<String, Object> getByUserName(String userName) {
        return settingMapper.getByUserName(userName);
    }

}
